#!/usr/bin/env node

const fetch = require("node-fetch");

let getData = async apiURL => {
  const message = await fetch(apiURL)
    .then(response => response.json())
    .catch(err => console.log(err));
  if (message.code === 200) {
    return message.data;
  } else {
    console.log(`Server Internal Error: ${message.message}`);
    return {};
  }
};

let postData = async (apiURL, body) => {
  const message = await fetch(apiURL, {
    method: "POST",
    body: JSON.stringify(body),
    headers: { "Content-Type": "application/json" }
  })
    .then(response => response.json())
    .catch(err => console.log(err));
  if (message.code === 200) {
    return message.data;
  } else {
    console.log(`${message.message}`);
  }
};

module.exports = {
  getData,
  postData
};
